<?php
namespace app\wap\controller;
class Login extends Base{
	
	public function index(){
		$this->add_step('登录');
		$this->show_bottom_bar = false;
		$this->is_foot_bartab  = false;
		$this->is_header       = true;
		$this->is_header_nav   = false;
		$user_type = $this->_get('user_type', '', 'user');
		$jumpurl   = session('login_jumpurl');
		if(!$jumpurl){
			$jumpurl = $_SERVER['HTTP_REFERER'];
			$jumpurl = $jumpurl ? $jumpurl : url('Question/index');
			$jumpurl = strpos($jumpurl, 'login/index') !== false ? url('Question/index') : $jumpurl;
			session('login_jumpurl', $jumpurl);
		}

		if($this->isPost()){
			$mobile    = $this->_post('mobile', '手机号不能为空！');
			$password  = $this->_post('password', '密码不能为空！');
			// $password  = md5($password);
			$user_info = model('User')->login($mobile, $password, 'wap');

			if($user_info){
				session('login_jumpurl', null);
				model('User')->save_session($user_info);
				$jumpurl = $jumpurl ? $jumpurl : url('Question/index');
				$this->success('登录成功！', $jumpurl);
			}else{
				$this->error(model('User')->tips_info);
			}
		}
		
		return $this->view();
	}
	// 微信一键登录
	public function weixin(){
		$jumpurl = session('login_jumpurl');
        if(!$jumpurl){
        	$jumpurl = $_SERVER['HTTP_REFERER'];
        	$jumpurl = $jumpurl ? $jumpurl : url('UserCenter/index');
			$jumpurl = strpos($jumpurl, 'login/index') !== false ? url('UserCenter/index') : $jumpurl;
            session('login_jumpurl', $jumpurl);
        }

		if(!$this->weixin_openid){
			session('weixin_login_jump', url('weixin'));
			$this->redirect(url('Login/get_weixin_openid'));
			// $this->success('正在登录微信用户...', U('Login/get_weixin_openid'));
		}

		// 获取数据库微信用户信息
		$wx_user       = model('Weixin')->get_openid_userinfo($this->weixin_openid);
		$unionid       = $wx_user['unionid'];
		$nickname      = $wx_user['nickname'];
		$headimgurl    = $wx_user['headimgurl'];
		$sex           = $wx_user['sex'];
		$province_name = $wx_user['province'];
		$city_name     = $wx_user['city'];
		// // 未关注的不处理
		// if($wx_user && $wx_user['is_delete']){
		// 	$this->error('对不起，您未关注公众号！');
		// }
		// 从微信接口上拉取用户信息
		if(!$wx_user){
			$wx_user       = model('Weixin')->weixin->get_openid_userinfo($this->weixin_openid);
			$unionid       = $wx_user['unionid'];
			$nickname      = $wx_user['nickname'];
			$headimgurl    = $wx_user['headimgurl'];
			$sex           = $wx_user['sex'];
			$province_name = $wx_user['province'];
			$city_name     = $wx_user['city'];
			$subscribe     = $wx_user['subscribe'];
		}

		$user_model = model('User');
		$user_model->setParams('head_url', $headimgurl);
		$user_model->setParams('sex', $sex);
		$user_model->setParams('province_name', $province_name);
		$user_model->setParams('city_name', $city_name);
		if($user_info = $user_model->sso_login('weixin', $this->weixin_openid, $nickname, true, 'wap')){
			//判断是否禁用
			if($user_info['status'] == 0){
				$this->error('您的账号被禁用，请联系相关人员！', url('UserCenter/index'));
			}
			model('User')->save_session($user_info);
			if($user_info['mobile']){
				session('login_jumpurl', null);
				$this->redirect($jumpurl);
			}else{
				$this->redirect(url('UserCenter/index'));
			}
		}
		$this->success('登录失败！', url('UserCenter/index'));

	}
	// 获取微信OPENID
	public function get_weixin_openid(){
		$is_weixin_pay = $this->_get('is_weixin_pay');
		$jumpurl = session('weixin_login_jump');
        if(!$jumpurl){
            session('weixin_login_jump', $_SERVER['HTTP_REFERER'] . '?is_weixin_pay=' . $is_weixin_pay);
        }
        if(!$this->is_weixin_visit){
            $this->error('请用微信客户端打开！');
        }
        // 初始化微信
		$weixin = model('Weixin')->weixin;
        if(!$this->weixin_openid){
            //获取微信认证CODE
            $code = $this->_get('code');

            if(!$code){
                $code_url = url('get_weixin_openid');
                $weixin->get_web_code($code_url, 'snsapi_userinfo');
            }else{
                //获取用户ID
                $openid = $weixin->get_code_openid($code);
                // var_dump('code:'.$code);
                // var_dump('openid:'.$openid);
                if($openid){
                    session('weixin_openid', $openid);
                    $this->weixin_openid = $openid;
                    //跳转至之前页面
                    $jumpurl = $jumpurl ? $jumpurl : url('UserCenter/index');
                    session('weixin_login_jump', null);
                    $this->redirect($jumpurl);
                }else{
                    $this->error('获取用户数据失败，请稍候再试！');
                }
            }
        }
	}
}